﻿
#include "Checklists.h"
#include "skyThread.h"

//double CL_brightness = 1;
#ifndef TOPCOLOR
#define TOPCOLOR EGEARGB(255, 130, 130, 130)
#endif // !TOPCOLOR

CLState currentState = CLState::TOPMENU;  // 初始显示主菜单

PIMAGE ckl = newimage();

int topX;
int topY;

void drawCL(int x0, int y0, double height, double cl_x, double cl_y) {
    // 整个窗口大小为900（width） * 1250（height）
    //setcaption("Checklists");

    int clwidth = height * 0.726887f;   // 857
    int clheight = height;              // 1179
    double clscale = height / 1179.0f;

    resize(ckl, clwidth, clheight);
    // 绘制到图层中去
    settarget(ckl);

    topX = cl_x;
    topY = cl_y;

    //setfillcolor(BLACK);
    //setfillcolor(WHITE);
    //ege_fillrect(x0, y0, clwidth, clheight);
    //setbkcolor(BLACK);

    // 绘制顶部按钮和标题（始终显示）
    clTopButton(x0, y0, clscale);
    clTitleTheme(x0, y0, clscale);

    // 根据当前状态决定绘制哪个界面
    switch (currentState) {
    case CLState::TOPMENU:
        clTopMenu(x0, y0, clscale);     // 主菜单界面    
        break;

    case CLState::USAGE:
        clUsage(x0, y0, clscale);     // UASGE界面
        break;

    case CLState::NORMAL:
        clNormal(x0, y0, clscale);      // 正常程序界面
        break;

    case CLState::POWER:
        clPower(x0, y0, clscale);      // POWER程序界面
        break;
    }

    settarget(NULL);
    //putimage(cl_x, cl_y, ckl);

}

void clTopButton(double x0, double y0, double clscale) {
    x0 -= 26 * clscale;
    y0 -= 35 * clscale;
    setcolor(EGEARGB(255, 104, 104, 104));
    setfillcolor(EGEARGB(255, 64, 64, 64));
    setlinewidth(3.0 * clscale);
    // 左边
    ege_fillrect(x0 + 52.0f * clscale, y0 + 132.0f * clscale, 148.0f * clscale, 35.0f * clscale);
    ege_rectangle(x0 + 55.0f * clscale, y0 + 135.0f * clscale, 142.0f * clscale, 29.0f * clscale);
    setfont(clscale * 30, 0, "Tahoma");
    setcolor(WHITE);
    settextjustify(LEFT_TEXT, TOP_TEXT);
    ege_drawtext("C/L", x0 + 60.0f * clscale, y0 + 135.0f * clscale);

    settextjustify(RIGHT_TEXT, TOP_TEXT);
    ege_drawtext("MENU", x0 + 192.0f * clscale, y0 + 135.0f * clscale);

    if (msg.x > topX + 26.0f * clscale && msg.x < topX + 172.0f * clscale
        && msg.y > topY + 94.0f * clscale && msg.y < topY + 130.0f * clscale) {
        setcolor(EGEARGB(255, 255, 255, 255));
        ege_rectangle(x0 + 52.0f * clscale, y0 + 132.0f * clscale, 148.0f * clscale, 35.0f * clscale);
        if (leftButtonDown) {
            // 根据点击位置设置对应的界面状态
            currentState = CLState::TOPMENU;
            leftButtonDown = false;  // 重置点击标志
        }
    }

    // 右边
    settextjustify(LEFT_TEXT, TOP_TEXT);
    setcolor(EGEARGB(255, 104, 104, 104));
    setfillcolor(EGEARGB(255, 64, 64, 64));
    setlinewidth(3.0 * clscale);
    ege_fillrect(x0 + 740.0f * clscale, y0 + 132.0f * clscale, 121.0f * clscale, 35.0f * clscale);
    ege_rectangle(x0 + 743.0f * clscale, y0 + 135.0f * clscale, 115.0f * clscale, 29.0f * clscale);
    setcolor(WHITE);
    settextjustify(CENTER_TEXT, TOP_TEXT);
    ege_drawtext("BACK", x0 + 801.0f * clscale, y0 + 135.0f * clscale);

    if (msg.x > topX + 718.0f * clscale && msg.x < topX + 838.0f * clscale
        && msg.y > topY + 94.0f * clscale && msg.y < topY + 130.0f * clscale) {
        setcolor(EGEARGB(255, 255, 255, 255));
        ege_rectangle(x0 + 740.0f * clscale, y0 + 132.0f * clscale, 121.0f * clscale, 35.0f * clscale);
    }
}

void clDownButton(double x0, double y0, double clscale) {
    setcolor(EGEARGB(255, 104, 104, 104));
    setfillcolor(EGEARGB(255, 64, 64, 64));
    setlinewidth(3.0 * clscale);
    // 左边
    ege_fillrect(x0 + 52.0f * clscale, y0 + 1083.0f * clscale, 122.0f * clscale, 35.0f * clscale);
    ege_rectangle(x0 + 55.0f * clscale, y0 + 1086.0f * clscale, 116.0f * clscale, 29.0f * clscale);
    setfont(clscale * 30, 0, "Tahoma");
    setcolor(WHITE);
    settextjustify(CENTER_TEXT, TOP_TEXT);
    ege_drawtext("RESET", x0 + 113.0f * clscale, y0 + 1085.0f * clscale);

    setcolor(EGEARGB(255, 104, 104, 104));
    ege_fillrect(x0 + 52.0f * clscale, y0 + 1134.0f * clscale, 122.0f * clscale, 35.0f * clscale);
    ege_rectangle(x0 + 55.0f * clscale, y0 + 1137.0f * clscale, 116.0f * clscale, 29.0f * clscale);
    settextjustify(CENTER_TEXT, TOP_TEXT);
    setcolor(WHITE);
    ege_drawtext("NEXT", x0 + 110.0f * clscale, y0 + 1136.0f * clscale);

    // 右边
    setcolor(EGEARGB(255, 104, 104, 104));
    ege_fillrect(x0 + 711.0f * clscale, y0 + 1134.0f * clscale, 147.0f * clscale, 35.0f * clscale);
    ege_rectangle(x0 + 714.0f * clscale, y0 + 1137.0f * clscale, 141.0f * clscale, 29.0f * clscale);
    settextjustify(CENTER_TEXT, TOP_TEXT);
    setcolor(WHITE);
    ege_drawtext("COMPLT", x0 + 785.0f * clscale, y0 + 1136.0f * clscale);
}

void clTitleTheme(double x0, double y0, double clscale) {
    x0 -= 26 * clscale;
    y0 -= 35 * clscale;
    setfillcolor(EGEARGB(255, 255, 255, 255));
    ege_fillrect(x0 + 47 * clscale, y0 + 203 * clscale, 813 * clscale, 23 * clscale);
}

void clTopMenu(double x0, double y0, double clscale) {
    x0 -= 26 * clscale;
    y0 -= 35 * clscale;
    setfont(clscale * 30, 0, "Tahoma");
    setcolor(TOPCOLOR);
    settextjustify(CENTER_TEXT, TOP_TEXT);
    ege_drawtext("TOP", x0 + 103.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("MENU", x0 + 181.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("0", x0 + 760.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("0", x0 + 796.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("0", x0 + 832.0f * clscale, y0 + 200.0f * clscale);
    settextjustify(LEFT_TEXT, TOP_TEXT);
    setcolor(EGEARGB(255, 255, 255, 255));
    ege_drawtext("INSTRUCTIONS", x0 + 68.0f * clscale, y0 + 258.0f * clscale);
    ege_drawtext("NORMAL  PROCEDURES", x0 + 68.0f * clscale, y0 + 284.0f * clscale);

    // 循环处理2个区域（根据你的需求调整数量）
    for (int i = 0; i < 2; i++) {
        float y_start = y0 + (260.0f + i * 26.0f) * clscale;
        float y_start1 = topY + (225.0f + i * 26.0f) * clscale;

        float y_end = y0 + (286.0f + i * 26.0f) * clscale;
        float y_end1 = topY + (251.0f + i * 26.0f) * clscale;


        if (msg.x > topX + 42.0f * clscale && msg.x < topX + 840.0f * clscale
            && msg.y > y_start1 && msg.y < y_end1) {
            setcolor(EGEARGB(255, 29, 242, 237));
            ege_rectangle(x0 + 64.0f * clscale, y_start, 790.0f * clscale, 26.0f * clscale);

            // 处理左键点击事件
            if (leftButtonDown) {
                // 根据点击位置设置对应的界面状态
                switch (i) {
                case 0: currentState = CLState::USAGE; break;     // 点击第一个区域进入USAGE界面
                case 1: currentState = CLState::NORMAL; break;     // 点击第二个区域进入POWER界面
                default: break;
                }
                leftButtonDown = false;  // 重置点击标志
            }
        }
    }
}

void clUsage(double x0, double y0, double clscale) {
    x0 -= 26 * clscale;
    y0 -= 35 * clscale;
    setfont(clscale * 30, 0, "Tahoma");
    setcolor(TOPCOLOR);
    settextjustify(CENTER_TEXT, TOP_TEXT);
    ege_drawtext("CHECKLIST", x0 + 130.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("USAGE", x0 + 240.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("1", x0 + 760.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("1", x0 + 796.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("1", x0 + 832.0f * clscale, y0 + 200.0f * clscale);

    settextjustify(LEFT_TEXT, TOP_TEXT);
    setcolor(EGEARGB(255, 255, 255, 255));
    setfont(clscale * 39, 0, "Tahoma");
    ege_drawtext("- The checklist will automatically monitor yout actions", x0 + 68.0f * clscale, y0 + 258.0f * clscale); // y += 34
    ege_drawtext("use the standard scroll keys and select key for checklist", x0 + 68.0f * clscale, y0 + 292.0f * clscale);
    ege_drawtext("manipulation or use the scroll and V button on the", x0 + 68.0f * clscale, y0 + 326.0f * clscale);
    ege_drawtext("pedestal", x0 + 68.0f * clscale, y0 + 360.0f * clscale);
    ege_drawtext("- The check items marked by the gray square on the", x0 + 68.0f * clscale, y0 + 394.0f * clscale);
    ege_drawtext("left require checking - select key on the item to check", x0 + 68.0f * clscale, y0 + 428.0f * clscale);
    ege_drawtext("it", x0 + 68.0f * clscale, y0 + 462.0f * clscale);
    ege_drawtext("- Any item can be overriden by selecting it", x0 + 68.0f * clscale, y0 + 496.0f * clscale);
    ege_drawtext("- The whole list can be overriden by selecting COMPLT", x0 + 68.0f * clscale, y0 + 530.0f * clscale);
    ege_drawtext("list can be reset by selecting RESET", x0 + 68.0f * clscale, y0 + 564.0f * clscale);
    clDownButton(x0, y0, clscale);
    if (msg.x > topX + 718.0f * clscale && msg.x < topX + 838.0f * clscale
        && msg.y > topY + 94.0f * clscale && msg.y < topY + 130.0f * clscale) {
        setcolor(EGEARGB(255, 255, 255, 255));
        ege_rectangle(x0 + 740.0f * clscale, y0 + 132.0f * clscale, 121.0f * clscale, 35.0f * clscale);
        if (leftButtonDown) {
            // 返回键
            currentState = CLState::TOPMENU;
            leftButtonDown = false;  // 重置点击标志
        }
    }
}

void clNormal(double x0, double y0, double clscale) {
    x0 -= 26 * clscale;
    y0 -= 35 * clscale;
    setfont(clscale * 30, 0, "Tahoma");
    setcolor(TOPCOLOR);
    settextjustify(LEFT_TEXT, TOP_TEXT);
    ege_drawtext("NORMAL", x0 + 78.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("PROCEDURES", x0 + 201.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("2", x0 + 760.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("0", x0 + 796.0f * clscale, y0 + 200.0f * clscale);
    ege_drawtext("0", x0 + 832.0f * clscale, y0 + 200.0f * clscale);
    settextjustify(LEFT_TEXT, TOP_TEXT);
    setcolor(EGEARGB(255, 255, 255, 255));
    std::string flightPhases[] = {
    "POWER",
    "COCKPIT PREPARATION",
    "BEFORE PUSHBACK&START",
    "ENGINE START",
    "AFTER START",
    "TAXI",
    "BEFORE TAKEOFF",
    "TAKEOFF",
    "AFTER TAKEOFF",
    "CLIMB",
    "CRUISE",
    "DESCENT PREPARATION",
    "DESCENT",
    "PRECISION APPROACH",
    "NON-PRECISION APPROACH WITH FLS",
    "NON-PRECISION APPROACH WITHOUT FLS",
    "VISUAL APPROACH",
    "LANDING",
    "GO AROUND",
    "AFTER LANDING",
    "PARKING",
    "SECURING THE AIRCRAFT"
    };
    double firsty = 258.0f;

    // 遍历并转换为宽字符
    for (const auto& phase : flightPhases) {
        // 计算所需宽字符长度（+1 包含终止符）
        int required = MultiByteToWideChar(CP_UTF8, 0, phase.c_str(), -1, nullptr, 0);
        if (required == 0) {
            //std::cerr << "Conversion error!" << std::endl;
            continue;
        }

        // 分配缓冲区并转换
        std::vector<wchar_t> wideBuffer(required);
        MultiByteToWideChar(CP_UTF8, 0, phase.c_str(), -1, wideBuffer.data(), required);

        // 获取 const wchar_t* 指针
        const wchar_t* wideStr = wideBuffer.data();
        ege_drawtext(wideStr, x0 + 68.0f * clscale, y0 + firsty * clscale);
        firsty += 26.0f;
    }

    // 循环处理22个区域（根据你的需求调整数量）
    for (int i = 0; i < 22; i++) {
        float y_start = y0 + (260.0f + i * 26.0f) * clscale;
        float y_start1 = topY + (225.0f + i * 26.0f) * clscale;

        float y_end = y0 + (286.0f + i * 26.0f) * clscale;
        float y_end1 = topY + (251.0f + i * 26.0f) * clscale;

        if (msg.x > topX + 718.0f * clscale && msg.x < topX + 838.0f * clscale
            && msg.y > topY + 94.0f * clscale && msg.y < topY + 130.0f * clscale) {
            setcolor(EGEARGB(255, 255, 255, 255));
            ege_rectangle(x0 + 740.0f * clscale, y0 + 132.0f * clscale, 121.0f * clscale, 35.0f * clscale);
            if (leftButtonDown) {
                // 返回键
                currentState = CLState::TOPMENU;
                leftButtonDown = false;  // 重置点击标志
            }
        }

        if (msg.x > topX + 42.0f * clscale && msg.x < topX + 840.0f * clscale
            && msg.y > y_start1 && msg.y < y_end1) {
            setcolor(EGEARGB(255, 29, 242, 237));
            ege_rectangle(x0 + 64.0f * clscale, y_start, 790.0f * clscale, 26.0f * clscale);

            // 处理左键点击事件
            if (leftButtonDown) {
                // 根据点击位置设置对应的界面状态
                switch (i) {
                case 0: currentState = CLState::POWER; break;     // 点击第一个区域进入USAGE界面
                case 1: currentState = CLState::NORMAL; break;     // 点击第二个区域进入POWER界面
                default: break;
                }
                leftButtonDown = false;  // 重置点击标志
            }
        }
    }
}

void clPower(double x0, double y0, double clscale) {
    x0 -= 26 * clscale;
    y0 -= 35 * clscale;
    if (msg.x > topX + 718.0f * clscale && msg.x < topX + 838.0f * clscale
        && msg.y > topY + 94.0f * clscale && msg.y < topY + 130.0f * clscale) {
        setcolor(EGEARGB(255, 255, 255, 255));
        ege_rectangle(x0 + 740.0f * clscale, y0 + 132.0f * clscale, 121.0f * clscale, 35.0f * clscale);
        if (leftButtonDown) {
            // 返回键
            currentState = CLState::NORMAL;
            leftButtonDown = false;  // 重置点击标志
        }
    }
}
